package com.contractpage.ec.service.impl;


import java.io.File;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.dao.AppointDao;
import com.contractpage.ec.dao.ConfirtrackDetailDao;
import com.contractpage.ec.dao.ContractDetailDao;
import com.contractpage.ec.dao.ContractStatusHistroyDao;
import com.contractpage.ec.dao.ExpressInformationDao;
import com.contractpage.ec.dao.ResourceDao;
import com.contractpage.ec.entity.Appoint;
import com.contractpage.ec.entity.ConfirtrackDetail;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.entity.ContractStatusHistroy;
import com.contractpage.ec.entity.ExpressInformation;
import com.contractpage.ec.entity.Resource;
import com.contractpage.ec.service.ContractDetailService;
import com.contractpage.util.StringUtils;



/**
 * 
 * ContractDetailServiceImpl数据库操作接口类
 * 
 **/
@Transactional
@Service
public class ContractDetailServiceImpl implements ContractDetailService{
	@Autowired
	private ContractDetailDao contractDetailDao;
	@Autowired
	private AppointDao appointDao;
	@Autowired
	private ContractStatusHistroyDao contractStatusHistroyDao;
	@Autowired
	private ConfirtrackDetailDao confirtrackDetailDao;
	
	@Autowired
	private ResourceDao resourceDao;
	
	@Autowired
	private ExpressInformationDao expressInformationDao;
	@Override
	public Page<ContractDetail> findListApps(Page<ContractDetail> page, ContractDetail entity) {
		entity.setPage(page);
		List<ContractDetail> list = contractDetailDao.findList(entity);
		for(ContractDetail detail : list){
			if(detail.getReserveNo() != null&&StringUtils.isNotEmpty(detail.getReserveNo().toString())){
				Appoint app = new Appoint();
				app.setReserveNo(detail.getReserveNo());
				detail.setApps(appointDao.findList(app));
			}
		}
		page.setList(list);
		return page;
	}
	@Override
	public Page<ContractDetail> findList(Page<ContractDetail> page,ContractDetail entity){
		entity.setPage(page);
		page.setList(contractDetailDao.findList(entity));
		return page;
	}
	@Override
	public int insert(ContractDetail entity){
		entity.preInsert("");
		return contractDetailDao.insert(entity);
	}
	@Override
	public int update(ContractDetail entity){
		entity.preUpdate();
		return contractDetailDao.update(entity);
	}
	@Override
	public int delete(ContractDetail entity){
		int i = contractDetailDao.delete(entity);
		return i;
	}
	@Override
	public int getSumDetail(ContractDetail entity) {
		return contractDetailDao.getSumDetail(entity);
	}
	/**
	 * 撤销分发
	 */
	@Override
	public Integer canselDistribute(ContractDetail entity) {
		// TODO Auto-generated method stub
		ContractDetail detail = new ContractDetail();
		detail.setId(entity.getId());
		detail.setStatus("2");
		detail.setReserveNo("");
		Appoint appoint = new Appoint();
		appoint.setId(entity.getAppoint().getId());
		appoint.setReserveNo(entity.getAppoint().getReserveNo());
		appoint.setStatus("0");
		appointDao.update(appoint);
		return contractDetailDao.update(detail);
	}
	@Override
	public Page<ContractDetail> findListExamine(Page<ContractDetail> page, ContractDetail entity) {
		// TODO Auto-generated method stub
		entity.setPage(page);
		page.setList(contractDetailDao.findListExamine(entity));
		return page;
	}
	@Override
	public int saveCustData(ContractDetail entity, String delids) {
		ConfirtrackDetail  confirtrackDetail = entity.getConfirtrackDetail();
		int i  = 0;
		if(StringUtils.isEmpty(entity.getConfirtrackDetail().getId())){
			confirtrackDetail.preInsert("");
			i = confirtrackDetailDao.insert(entity.getConfirtrackDetail());
			for(Resource resources : entity.getResource()){
				resources.setUpdateBy(entity.getCreateBy());
				resources.preInsert("");
				resourceDao.insert(resources);
			}
		}
		else{
			i = confirtrackDetailDao.update(entity.getConfirtrackDetail());
			Resource  res = new Resource();
			res.setLinkid(entity.getId());
			if(StringUtils.isNoneEmpty(delids)){
				String[] id = delids.split(",");
				for (String str : id) {
					Resource delres = new Resource();
					delres.setId(str);
					Resource queryRes = resourceDao.get(delres);
					File file = new File(queryRes.getUrl());
			        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
			        if (file.exists() && file.isFile()) {
			        	file.delete();
			        }
					resourceDao.delete(delres);
				}
			}
			for(Resource resources : entity.getResource()){
				resources.setUpdateBy(entity.getCreateBy());
				resources.preInsert("");
				resourceDao.insert(resources);
			}
			
		}
		 
		entity.setConfirtrackstatus("1");
		contractDetailDao.updateAudit(entity);
		if(i>0){
			contractStatusHistroyDao.insert(buildStatusHistroy(entity));
		}
		return i;
	}
	private ContractStatusHistroy buildStatusHistroy(ContractDetail detail){
		ContractStatusHistroy histroy = new ContractStatusHistroy();
		histroy.setDetailid(detail.getId());
		Integer sequence = contractStatusHistroyDao.getMaxSequence(histroy);
		histroy.setSequence(sequence==null?1:sequence + 1);
		histroy.preInsert("");
		histroy.setConfirtrackstatus(detail.getConfirtrackstatus()==null?"0":detail.getConfirtrackstatus());
		histroy.setRemark(detail.getRemarks());
		histroy.setStatus(detail.getStatus());
		histroy.setCreateBy(detail.getCreateBy());
		histroy.setUpdateDate(new Date());
		return histroy;
	}
	@Override
	public ContractDetail findContractDetail(ContractDetail entity) {
		// TODO Auto-generated method stub
		Resource resource = new Resource();
		ContractDetail contractDetail = new ContractDetail();
		ConfirtrackDetail confirtrackDetail = new ConfirtrackDetail();
		confirtrackDetail.setContractid(entity.getId());
		confirtrackDetail = confirtrackDetailDao.get(confirtrackDetail);
		resource.setLinkid(entity.getId());
		List<Resource> list= resourceDao.findList(resource);
		contractDetail.setConfirtrackDetail(confirtrackDetail);
		contractDetail.setResource(list);
		return contractDetail;
	}
	@Override
	public int saveAuditData(ContractDetail entity) {
		// TODO Auto-generated method stub
		entity.setConfirtrackstatus("2");
		int i =contractDetailDao.updateAudit(entity);
		if(i>0){
			contractStatusHistroyDao.insert(buildStatusHistroy(entity));
		}
		return i;
	}
	@Override
	public int setAuditData(ContractDetail entity) {
		entity.setUpdateBy(entity.getCreateBy());
		entity.setAuditdate(new Date());
		int i =contractDetailDao.updateAudit(entity);
		if(i>0){
			contractStatusHistroyDao.insert(buildStatusHistroy(entity));
		}
		// TODO Auto-generated method stub
		return i;
	}
	@Override
	public int sendContractDetail(ContractDetail entity,ExpressInformation  expressInformation, String ids) {
		String[] id = ids.split(",");
		int number = 0;
		if(expressInformation == null){
			for (String detailId : id) {
			ContractDetail detail = new ContractDetail();
			detail.setId(detailId);
			detail.setConfirtrackstatus(entity.getConfirtrackstatus());
			detail.setConfirtracknumber("");
			detail.setConfiruser(entity.getUpdateBy()==null?"":entity.getUpdateBy().getCode());
			number +=contractDetailDao.update(detail);
			}
			return number;
		}
		// TODO Auto-generated method stub
		for (String detailId : id) {
			ContractDetail detail = new ContractDetail();
			detail.setId(detailId);
			detail.setConfirtrackstatus(entity.getConfirtrackstatus());
			detail.setConfirtracknumber(entity.getConfirtracknumber());
			detail.setConfiruser(entity.getUpdateBy()==null?"":entity.getUpdateBy().getCode());
			detail.setIssuedate(new Date());
			number +=contractDetailDao.update(detail);
		}
		expressInformation.setSends(number);
		expressInformation.setTracknumber(entity.getConfirtracknumber());
		expressInformation.setStatus("0");
		expressInformation.setUpdateBy(expressInformation.getCreateBy());
		expressInformation.setUpdateDate(new Date());
		expressInformation.setSignDate(null);
		expressInformation.setType("3");
		expressInformation.preInsert("");
		expressInformationDao.insert(expressInformation);
		return number;
	}
	/**
	 * 问题合同撤回
	 */
	@Override
	public Integer problemCansel(ContractDetail entity) {
		// TODO Auto-generated method stub
		entity = contractDetailDao.get(entity);
		entity.setStatus("5");
		entity.setProblemtracknumber("");
		contractStatusHistroyDao.insert(buildStatusHistroy(entity));
		return contractDetailDao.update(entity);
	}
	@Override
	public int setAllAuditData(String ids) {
		// TODO Auto-generated method stub
		int i = 0;
		String[] id = ids.split(",");
		for (String str : id) {
			ContractDetail entity = new ContractDetail();
			entity.setId(str);
			entity.setUpdateBy(entity.getCreateBy());
			entity.setAuditdate(new Date());
			i +=contractDetailDao.updateAudit(entity);
				contractStatusHistroyDao.insert(buildStatusHistroy(entity));
			// TODO Auto-generated method stub			
			
		}
		return i;
	}
}